<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深入理解Spring循环依赖 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.8;
        }
        .serif {
            font-family: 'Noto Serif SC', serif;
        }
        .hero-bg {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .highlight {
            background: linear-gradient(120deg, rgba(251, 191, 36, 0.2) 0%, rgba(251, 191, 36, 0) 100%);
            padding: 0.2rem 0.4rem;
            border-radius: 0.2rem;
        }
        .drop-cap::first-letter {
            float: left;
            font-size: 4.5rem;
            line-height: 0.65;
            margin: 0.1em 0.2em 0 0;
            color: #4f46e5;
            font-weight: 700;
        }
        .mermaid {
            background: white;
            padding: 2rem;
            border-radius: 0.5rem;
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-bg text-white py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-5xl px-4">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-2/3 mb-10 md:mb-0">
                    <h1 class="text-4xl md:text-5xl font-bold mb-6 serif">深入理解Spring循环依赖</h1>
                    <p class="text-xl md:text-2xl opacity-90 mb-8">探索依赖注入背后的机制与解决方案</p>
                    <div class="bg-white bg-opacity-20 p-6 rounded-lg backdrop-filter backdrop-blur-sm">
                        <p class="drop-cap">循环依赖是Spring开发中常见但容易引起困惑的问题。了解其工作机制将帮助你设计更优雅的应用程序架构，避免潜在的陷阱。</p>
                    </div>
                </div>
                <div class="md:w-1/3 flex justify-center">
                    <div class="bg-white bg-opacity-20 rounded-full p-6 backdrop-filter backdrop-blur-sm">
                        <i class="fas fa-sync-alt text-6xl md:text-8xl text-white animate-spin-slow"></i>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <div class="container mx-auto max-w-5xl px-4 py-16">
        <!-- Definition Section -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <div class="bg-indigo-100 w-12 h-12 rounded-full flex items-center justify-center mr-4">
                    <i class="fas fa-info-circle text-indigo-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold serif">循环依赖的定义</h2>
            </div>
            
            <p class="text-lg mb-8">循环依赖指的是在对象的创建过程中，出现了两个或多个对象互相依赖的情况。这种情况在 Spring 中比较常见，特别是在使用依赖注入（DI）时。如果处理不当，循环依赖可能导致容器无法完成对象的创建，进而影响整个应用程序的正常运行。</p>
            
            <div class="grid md:grid-cols-2 gap-8 mb-8">
                <div class="bg-white p-6 rounded-xl shadow-md card-hover border-l-4 border-red-500">
                    <div class="flex items-center mb-4">
                        <div class="bg-red-100 w-10 h-10 rounded-full flex items-center justify-center mr-3">
                            <i class="fas fa-exclamation-triangle text-red-600"></i>
                        </div>
                        <h3 class="text-xl font-bold">构造器循环依赖</h3>
                    </div>
                    <p>这种依赖发生在使用构造函数注入时。例如，<span class="highlight">Bean A</span> 依赖 <span class="highlight">Bean B</span> 的构造函数，而 <span class="highlight">Bean B</span> 又依赖 <span class="highlight">Bean A</span> 的构造函数。这种依赖会导致 Spring 容器无法实例化这些 Bean，因为它们相互等待对方的创建。</p>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-md card-hover border-l-4 border-green-500">
                    <div class="flex items-center mb-4">
                        <div class="bg-green-100 w-10 h-10 rounded-full flex items-center justify-center mr-3">
                            <i class="fas fa-check-circle text-green-600"></i>
                        </div>
                        <h3 class="text-xl font-bold">Setter 循环依赖</h3>
                    </div>
                    <p>这种依赖发生在使用 setter 方法注入时。例如，<span class="highlight">Bean A</span> 通过 setter 方法注入 <span class="highlight">Bean B</span>，而 <span class="highlight">Bean B</span> 通过 setter 方法注入 <span class="highlight">Bean A</span>。这种依赖通常比构造器循环依赖更容易处理，因为 Spring 容器可以先实例化这些 Bean，然后再进行依赖注入。</p>
                </div>
            </div>
            
            <div class="mermaid">
                graph LR
                    A[Bean A] -->|构造器依赖| B[Bean B]
                    B -->|构造器依赖| A
                    style A fill:#fecaca,stroke:#333
                    style B fill:#bbf7d0,stroke:#333
            </div>
        </section>

        <!-- Mechanism Section -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <div class="bg-purple-100 w-12 h-12 rounded-full flex items-center justify-center mr-4">
                    <i class="fas fa-cogs text-purple-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold serif">处理循环依赖的机制</h2>
            </div>
            
            <div class="mb-8">
                <div class="bg-white p-6 rounded-xl shadow-md mb-6">
                    <h3 class="text-xl font-bold mb-4 flex items-center">
                        <span class="bg-yellow-100 w-8 h-8 rounded-full flex items-center justify-center mr-3">
                            <i class="fas fa-times text-yellow-600"></i>
                        </span>
                        构造器注入的限制
                    </h3>
                    <p>Spring 容器无法解决构造器循环依赖。这是因为在构造器注入过程中，Spring 必须完全构造 Bean 实例才能满足所有依赖，这在循环依赖的情况下是无法完成的。对于构造器注入，Spring 会抛出 <code class="bg-gray-100 px-2 py-1 rounded">BeanCurrentlyInCreationException</code> 异常。</p>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-md">
                    <h3 class="text-xl font-bold mb-4 flex items-center">
                        <span class="bg-blue-100 w-8 h-8 rounded-full flex items-center justify-center mr-3">
                            <i class="fas fa-check text-blue-600"></i>
                        </span>
                        Setter 注入的三级缓存机制
                    </h3>
                    <p>Spring 容器可以通过使用三级缓存机制来解决 setter 循环依赖问题。具体流程如下：</p>
                    
                    <div class="mt-4 space-y-4">
                        <div class="flex items-start">
                            <div class="bg-blue-500 text-white rounded-full w-6 h-6 flex items-center justify-center flex-shrink-0 mt-1 mr-3">1</div>
                            <div>
                                <h4 class="font-bold">创建 Bean 实例</h4>
                                <p class="text-gray-700">Spring 容器在初始化 Bean 时，首先创建一个 Bean 实例（但尚未完成所有依赖注入）。这个阶段的 Bean 实例会被放入一个称为 "正在创建中的 Bean" 的缓存中。</p>
                            </div>
                        </div>
                        
                        <div class="flex items-start">
                            <div class="bg-blue-500 text-white rounded-full w-6 h-6 flex items-center justify-center flex-shrink-0 mt-1 mr-3">2</div>
                            <div>
                                <h4 class="font-bold">设置属性</h4>
                                <p class="text-gray-700">Spring 容器随后会对 Bean 进行属性注入。如果在注入过程中发现该 Bean 依赖于其他尚未完成初始化的 Bean，Spring 容器会尝试从缓存中获取正在创建中的 Bean。</p>
                            </div>
                        </div>
                        
                        <div class="flex items-start">
                            <div class="bg-blue-500 text-white rounded-full w-6 h-6 flex items-center justify-center flex-shrink-0 mt-1 mr-3">3</div>
                            <div>
                                <h4 class="font-bold">完成初始化</h4>
                                <p class="text-gray-700">当所有依赖都被注入完成后，Spring 容器将 Bean 从缓存中移除，并完成 Bean 的初始化。</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="mermaid">
                graph TD
                    A[创建Bean实例] --> B[放入三级缓存]
                    B --> C[属性注入]
                    C --> D{发现依赖?}
                    D -->|是| E[从缓存获取依赖]
                    E --> C
                    D -->|否| F[完成初始化]
                    F --> G[从缓存移除]
            </div>
        </section>

        <!-- Solutions Section -->
        <section>
            <div class="flex items-center mb-8">
                <div class="bg-green-100 w-12 h-12 rounded-full flex items-center justify-center mr-4">
                    <i class="fas fa-lightbulb text-green-600 text-xl"></i>
                </div>
                <h2 class="text-3xl font-bold serif">循环依赖的解决方案</h2>
            </div>
            
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-white p-6 rounded-xl shadow-md card-hover">
                    <div class="bg-indigo-100 w-12 h-12 rounded-full flex items-center justify-center mb-4 mx-auto">
                        <i class="fas fa-code-branch text-indigo-600 text-xl"></i>
                    </div>
                    <h3 class="text-xl font-bold text-center mb-3">重构设计</h3>
                    <p class="text-center">通过重构代码设计来避免循环依赖。例如，将互相依赖的 Bean 抽象成接口，或者使用依赖注入的替代方案。</p>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-md card-hover">
                    <div class="bg-yellow-100 w-12 h-12 rounded-full flex items-center justify-center mb-4 mx-auto">
                        <i class="fas fa-clock text-yellow-600 text-xl"></i>
                    </div>
                    <h3 class="text-xl font-bold text-center mb-3">使用 @Lazy 注解</h3>
                    <p class="text-center">在某些情况下，可以使用 <code class="bg-gray-100 px-1 py-0.5 rounded">@Lazy</code> 注解来延迟 Bean 的初始化，从而解决循环依赖问题。</p>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-md card-hover">
                    <div class="bg-purple-100 w-12 h-12 rounded-full flex items-center justify-center mb-4 mx-auto">
                        <i class="fas fa-project-diagram text-purple-600 text-xl"></i>
                    </div>
                    <h3 class="text-xl font-bold text-center mb-3">方法注入</h3>
                    <p class="text-center">通过方法注入的方式来解决依赖问题。这种方法允许将 Bean 的依赖注入推迟到 Bean 实例化之后的阶段。</p>
                </div>
            </div>
            
            <div class="mt-10 bg-blue-50 p-6 rounded-xl border border-blue-200">
                <h3 class="text-xl font-bold mb-4 flex items-center">
                    <i class="fas fa-question-circle text-blue-500 mr-3"></i>
                    最佳实践建议
                </h3>
                <ul class="space-y-2 pl-5">
                    <li class="relative pl-6 before:absolute before:left-0 before:top-2 before:w-2 before:h-2 before:bg-blue-500 before:rounded-full">优先使用 setter 注入而非构造器注入</li>
                    <li class="relative pl-6 before:absolute before:left-0 before:top-2 before:w-2 before:h-2 before:bg-blue-500 before:rounded-full">保持 Bean 的职责单一，避免过于复杂的依赖关系</li>
                    <li class="relative pl-6 before:absolute before:left-0 before:top-2 before:w-2 before:h-2 before:bg-blue-500 before:rounded-full">考虑使用事件驱动架构来解耦组件</li>
                    <li class="relative pl-6 before:absolute before:left-0 before:top-2 before:w-2 before:h-2 before:bg-blue-500 before:rounded-full">定期审查依赖关系，识别潜在的循环依赖风险</li>
                </ul>
            </div>
        </section>
    </div>

    <!-- Footer -->
    <footer class="bg-gray-900 text-white py-12">
        <div class="container mx-auto max-w-5xl px-4">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-6 md:mb-0">
                    <h3 class="text-2xl font-bold mb-2">技术小馆</h3>
                    <p class="text-gray-400">探索技术的无限可能</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-gray-300 hover:text-white transition-colors duration-300 flex items-center">
                        <i class="fas fa-link mr-2"></i> http://www.yuque.com/jtostring
                    </a>
                </div>
            </div>
            <div class="border-t border-gray-800 mt-8 pt-8 text-center text-gray-500 text-sm">
                © 2023 技术小馆. 保留所有权利.
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>